package org.testis.data.parser;

import org.testis.data.TestStepDataObject;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.webautomationmodule.data.WebAutomationModuleDataObject;
import org.testis.webautomationmodule.ui.UIEnumaration.UIObjectParameter;
import org.testis.webautomationmodule.ui.UIEnumaration.UIObjectType;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;

import org.testis.orm.refinedclasses.DynamicMapDesignTreeNode;
import org.testis.orm.refinedclasses.DynamicMapObject;
import org.testis.orm.refinedclasses.DynamicMapParameter;


/**
 *
 * @author nurhat
 */
public class WAMSetAndGetDopDecorator extends WAMDopDecorator
{

    public WAMSetAndGetDopDecorator(
        WebAutomationModuleDataObject data,
        DynamicMapDesignTreeNode root)
    {
        super(data,
              root);
    }


    private TestStepDataObject parsePageObjects()
    {

        ArrayList<DynamicMapDesignTreeNode> objects =
            createPageDataObjects(getData());
        appendObjectsToList(objects,
                            getData().getPageObjectsDataList());
        LogSenderService.putMessageOnQueue("TestStepDataObjectParser",
                                           AtofLogLevel.INFO,
                                           "Parsed data object as WebAutomationDataObject ",
                                           getData().getMessageId());

        return getData();
    }


    public String pathToString(ArrayList<DynamicMapDesignTreeNode> path)
    {
        String output = "";
        for (int i = 0; i < path.size(); i++) {
            output = path.get(i).getId() + output;
        }
        return output;
    }


    private ArrayList<DynamicMapDesignTreeNode> createPageDataObjects(
        TestStepDataObject data)
    {
        ArrayList<DynamicMapDesignTreeNode> lst =
            new ArrayList<DynamicMapDesignTreeNode>();
        Set keys = getData().getDataToApply().keySet();
        Iterator It = keys.iterator();
        String currentPath = "";
        String objPath;
        while (It.hasNext()) {
            String current = (String) (It.next());
            DynamicMapDesignTreeNode obj = findObject(current);
            if (obj != null) {
                ArrayList<DynamicMapDesignTreeNode> path =
                    new ArrayList<DynamicMapDesignTreeNode>();
                findObjectPath(path,
                               obj.getParent());
                objPath = pathToString(path);
                boolean isObjectInPopupWindow = isObjectInPopupWindow(path);

                if (!currentPath.equals(objPath) && !isObjectInPopupWindow) {
                    processObjectPath(path,
                                      lst);
                    currentPath = objPath;
                }
                else if (isObjectInPopupWindow) {
                    processObjectPath(path,
                                      lst);
                }
                lst.add(obj);
                addBehaviors(lst,
                             obj);
            }
            else {
                LogSenderService.putMessageOnQueue("TestStepDataObjectParser",
                                                   AtofLogLevel.WARN,
                                                   "Object not found with id " +
                                                   current,
                                                   data.getMessageId());
            }
        }

        for (int i = 0; i < lst.size();
            i++) {
            System.out.println(lst.get(i).getObject().getTypeName() + ":" +
                               lst.get(i).getObject().getName());
        }
        return lst;

    }


    private void processObjectPath(ArrayList<DynamicMapDesignTreeNode> path,
                                   ArrayList<DynamicMapDesignTreeNode> listToAdd)
    {
        DynamicMapObject obj;
        String objType;
        DynamicMapParameter objName;
        for (int i = 0; i < path.size(); i++) {
            obj = path.get(i).getObject();
            objType = obj.getTypeName();
            objName =
                obj.getParameterByName(UIObjectParameter.AbstractId.name());
            if (!(objType.equals(UIObjectType.UIDeviceMapContainer.name()) ||
                objType.equals(UIObjectType.UIPage.name()) ||
                objType.equals(UIObjectType.UIDynamicGrid.name()))) {
                listToAdd.add(path.get(i));
                addBehaviors(listToAdd,
                             path.get(i));
            }
            if (objType.equals(UIObjectType.UIPage.name())) {

                DynamicMapDesignTreeNode pageLink =
                    getRoot().searchObjectByParameterValue(
                    UIObjectParameter.TargetPageId.name(),
                    objName.getValue(),
                    UIObjectType.UILink.name());
                if (pageLink != null) {
                    ArrayList<DynamicMapDesignTreeNode> linkPath =
                        new ArrayList<DynamicMapDesignTreeNode>();
                    findObjectPath(linkPath,
                                   pageLink.getParent());
                    processObjectPath(linkPath,
                                      listToAdd);
                    listToAdd.add(pageLink);
                    addBehaviors(listToAdd,
                                 pageLink);

                    DynamicMapParameter targetFrame =
                        pageLink.getObject().getParameterByName(
                        UIObjectParameter.TargetFrameId.name());
                    DynamicMapDesignTreeNode resultFrame =
                        getRoot().searchObjectByParameterValue(
                        UIObjectParameter.AbstractId.name(),
                        targetFrame.getValue(),
                        UIObjectType.UIFrame.name());
                    if (resultFrame != null) {
                        ArrayList<DynamicMapDesignTreeNode> framePath =
                            new ArrayList<DynamicMapDesignTreeNode>();
                        findObjectPath(framePath,
                                       resultFrame.
                                       getParent());
                        listToAdd.add(resultFrame);
                        addBehaviors(listToAdd,
                                     resultFrame);
                    }
                }
            }
        }
    }


    @Override
    protected boolean setBehavior()
    {
        if (parsePageObjects() != null) {
            return true;
        }
        else {
            return false;
        }
    }
}
